﻿using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.UI.ViewEditor;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using NUnit.Framework;
using Rhino.Mocks;

namespace FireStarterModeller.Test.UI.ViewEditor
{
     [TestFixture]
    public class TestUIViewSelectorControl
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
        }

        private static IControlFactory GetControlFactory()
        {
            ControlFactoryWin controlFactory = MockRepository.GeneratePartialMock<ControlFactoryWin>();
            controlFactory.Stub(win => win.ShowMessageBox("Are you sure you want to delete this UI View?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Stop)).Return(DialogResult.Yes);
            return controlFactory;
        }

         [Test]
         public void TestUIViews()
         {
             //---------------Set up test pack-------------------
             DMSolution dmSolution = TestUtilsShared.CreateSavedDMSolution();
             DMClass dmClass = dmSolution.DefaultAssembly.CreateClass(TestUtilsShared.GetRandomString());
             UIView uiView = dmClass.UIViews.CreateBusinessObject();
             uiView.ViewName = TestUtilsShared.GetRandomString();
             UIViewSelectorControl control = new UIViewSelectorControl(GetControlFactory(), new DMToClassDefsXmlConverter());

             //---------------Execute Test ----------------------
             control.UIViews = dmClass.UIViews;

             //---------------Test Result -----------------------
             Assert.AreEqual(1, control.UIViews.Count);
             Assert.IsTrue(control.UIViewComboBox.Items.Contains(uiView));
             //---------------Tear Down -------------------------          
         }

         [Test]
         public void TestSelectedUIView_Get()
         {
             //---------------Set up test pack-------------------
             DMSolution dmSolution = TestUtilsShared.CreateSavedDMSolution();
             DMClass dmClass = dmSolution.DefaultAssembly.CreateClass(TestUtilsShared.GetRandomString());
             UIView uiView1 = dmClass.UIViews.CreateBusinessObject();
             uiView1.ViewName = TestUtilsShared.GetRandomString();
             UIView uiView2 = dmClass.UIViews.CreateBusinessObject();
             uiView2.ViewName = TestUtilsShared.GetRandomString();
             UIViewSelectorControl control = new UIViewSelectorControl(GetControlFactory(), new DMToClassDefsXmlConverter());

             //---------------Execute Test ----------------------
             control.UIViews = dmClass.UIViews;
             UIView selectedUIView = control.SelectedUIView;

             //---------------Test Result -----------------------
             Assert.AreEqual(2, control.UIViews.Count);
             Assert.AreSame(uiView1, selectedUIView);
             //---------------Tear Down -------------------------          
         }

         [Test]
         public void TestSelectedUIView_Set()
         {
             //---------------Set up test pack-------------------
             DMSolution dmSolution = TestUtilsShared.CreateSavedDMSolution();
             DMClass dmClass = dmSolution.DefaultAssembly.CreateClass(TestUtilsShared.GetRandomString());
             UIView uiView1 = dmClass.UIViews.CreateBusinessObject();
             uiView1.ViewName = TestUtilsShared.GetRandomString();
             UIView uiView2 = dmClass.UIViews.CreateBusinessObject();
             uiView2.ViewName = TestUtilsShared.GetRandomString();
             UIViewSelectorControl control = new UIViewSelectorControl(GetControlFactory(), new DMToClassDefsXmlConverter());
             control.UIViews = dmClass.UIViews;
             UIView selectedUIView = null;
             control.UIViewSelected += (sender, args) => selectedUIView = args.BusinessObject;
             //---------------Assert PreConditions---------------      
             Assert.AreSame(uiView1, control.SelectedUIView);
             //---------------Execute Test ----------------------
             control.SelectedUIView = uiView2;

             //---------------Test Result -----------------------
             Assert.AreSame(uiView2, control.SelectedUIView);
             Assert.AreSame(uiView2, selectedUIView);
             //---------------Tear Down -------------------------          
         }

         [Test]
         public void TestCreateNewUIView()
         {
             //---------------Set up test pack-------------------
             DMSolution dmSolution = TestUtilsShared.CreateSavedDMSolution();
             DMClass dmClass = dmSolution.DefaultAssembly.CreateClass(TestUtilsShared.GetRandomString());
             UIView uiView1 = dmClass.UIViews.CreateBusinessObject();
             string viewName = TestUtilsShared.GetRandomString();
             uiView1.ViewName = viewName;
             UIViewSelectorControl control = new UIViewSelectorControl(GetControlFactory(), new DMToClassDefsXmlConverter());
             control.UIViews = dmClass.UIViews;
             control.SelectedUIView = uiView1;
             //---------------Assert PreConditions---------------            
             Assert.AreEqual(1, dmClass.UIViews.Count);
             Assert.AreEqual(viewName, dmClass.UIViews[0].ViewName);
             Assert.AreSame(uiView1, control.SelectedUIView);
             //---------------Execute Test ----------------------
             control.CreateNewButton.PerformClick();
             //---------------Test Result -----------------------
             Assert.AreEqual(2, dmClass.UIViews.Count);
             Assert.AreEqual(viewName, dmClass.UIViews[0].ViewName);
             Assert.AreEqual("NewView", dmClass.UIViews[1].ViewName);
             Assert.AreSame(dmClass.UIViews[1], control.SelectedUIView);
             //---------------Tear Down -------------------------          
         }

         [Test]
         public void TestCreateNewUIView_TwoUIViews()
         {
             //---------------Set up test pack-------------------
             DMSolution dmSolution = TestUtilsShared.CreateSavedDMSolution();
             DMClass dmClass = dmSolution.DefaultAssembly.CreateClass(TestUtilsShared.GetRandomString());
             UIViewSelectorControl control = new UIViewSelectorControl(GetControlFactory(), new DMToClassDefsXmlConverter());
             control.UIViews = dmClass.UIViews;
             //control.SelectedUIView = uiView1;
             control.CreateNewButton.PerformClick();
             //---------------Assert PreConditions---------------            
             Assert.AreEqual(1, dmClass.UIViews.Count);
             Assert.AreEqual("NewView", dmClass.UIViews[0].ViewName);
             Assert.AreSame(dmClass.UIViews[0], control.SelectedUIView);
             //---------------Execute Test ----------------------
             control.CreateNewButton.PerformClick();
             //---------------Test Result -----------------------
             Assert.AreEqual(2, dmClass.UIViews.Count);
             Assert.AreEqual("NewView1", dmClass.UIViews[1].ViewName);
             Assert.AreSame(dmClass.UIViews[1], control.SelectedUIView);
             //---------------Tear Down -------------------------          
         }

         [Test]
         public void TestCreateNewUIView_WhenNoSelectedUIView_ShouldCreateEmptyView()
         {
             //---------------Set up test pack-------------------
             DMSolution dmSolution = TestUtilsShared.CreateSavedDMSolution();
             DMClass dmClass = dmSolution.DefaultAssembly.CreateClass(TestUtilsShared.GetRandomString());
             UIViewSelectorControl control = new UIViewSelectorControl(GetControlFactory(), new DMToClassDefsXmlConverter());
             control.UIViews = dmClass.UIViews;
             control.SelectedUIView = null;
             //---------------Assert PreConditions---------------            
             Assert.AreEqual(0, dmClass.UIViews.Count);
             Assert.IsNull(control.SelectedUIView);
             //---------------Execute Test ----------------------
             control.CreateNewButton.PerformClick();
             //---------------Test Result -----------------------
             Assert.AreEqual(1, dmClass.UIViews.Count);
             UIView uiView = dmClass.UIViews[0];
             Assert.AreEqual("NewView", uiView.ViewName);
             Assert.AreSame(uiView, control.SelectedUIView);
             Assert.IsNull(uiView.UIFormInfo);
             Assert.IsNull(uiView.UIGridInfo);
             //---------------Tear Down -------------------------          
         }

         [Test]
         public void Test_DeleteUIView()
         {
             //---------------Set up test pack-------------------
             DMSolution dmSolution = TestUtilsShared.CreateSavedDMSolution();
             DMClass dmClass = dmSolution.DefaultAssembly.CreateClass(TestUtilsShared.GetRandomString());
             UIView uiView1 = dmClass.UIViews.CreateBusinessObject();
             string viewName = TestUtilsShared.GetRandomString();
             uiView1.ViewName = viewName;
             UIViewSelectorControl control = new UIViewSelectorControl(GetControlFactory(), new DMToClassDefsXmlConverter());
             control.UIViews = dmClass.UIViews;
             control.SelectedUIView = uiView1;
             bool eventFired = false;
             control.UIViewSelected += (sender, args) => eventFired = true;
             //---------------Assert PreConditions---------------            
             Assert.AreEqual(1, dmClass.UIViews.Count);
             Assert.AreEqual(viewName, dmClass.UIViews[0].ViewName);
             Assert.AreSame(uiView1, control.SelectedUIView);
             //---------------Execute Test ----------------------
             control.DeleteButton.PerformClick();
             //---------------Test Result -----------------------
             Assert.AreEqual(0, dmClass.UIViews.Count);
             Assert.AreSame(null, control.SelectedUIView);
             Assert.IsTrue(eventFired);
             //---------------Tear Down -------------------------          

         }        
         
         [Test]
         public void Test_DeleteUIView_WhenTwoOrMore()
         {
             //---------------Set up test pack-------------------
             DMSolution dmSolution = TestUtilsShared.CreateSavedDMSolution();
             DMClass dmClass = dmSolution.DefaultAssembly.CreateClass(TestUtilsShared.GetRandomString());
             UIView uiView1 = dmClass.UIViews.CreateBusinessObject();
             UIView uiView2 = dmClass.UIViews.CreateBusinessObject();
             string viewName = TestUtilsShared.GetRandomString();
             uiView1.ViewName = viewName;
             UIViewSelectorControl control = new UIViewSelectorControl(GetControlFactory(), new DMToClassDefsXmlConverter());
             control.UIViews = dmClass.UIViews;
             control.SelectedUIView = uiView1;
             //---------------Assert PreConditions---------------            
             Assert.AreEqual(2, dmClass.UIViews.Count);
             Assert.AreEqual(viewName, dmClass.UIViews[0].ViewName);
             Assert.AreSame(uiView1, control.SelectedUIView);
             //---------------Execute Test ----------------------
             control.DeleteButton.PerformClick();
             //---------------Test Result -----------------------
             Assert.AreEqual(1, dmClass.UIViews.Count);
             Assert.AreSame(uiView2, control.SelectedUIView);
             //---------------Tear Down -------------------------          

         }
    }
}
